package atom.ui 
{
	import atom.util.Collection;
	import flash.display.Sprite;
	
	/**
	 * Represents a container for children views
	 * @author Jorge Braccini
	 */
	public class ViewContainer extends View {
		
		protected var _views:Array;
		
		public function ViewContainer(init:Object = null) {
			super(init);
			
			_views = [];
		}
		
		/////////////////////////////////////////
		// Public Methods ///////////////////////
		/////////////////////////////////////////
		
		public function addView(view:View):void {
			view.viewParent = this;
			
			_views.push(view);
			addChild(view);
		}
		
		public function removeView(view:View):void {
			if (containsView(view)) {
				view.viewParent = null;
				
				_views.splice(_views.indexOf(view), 1);
				removeChild(view);
			}
		}
		
		override public function invalidate():void 	{
			invalidateAll();
			super.invalidate();
		}
		
		public function containsView(view:View):Boolean {
			var r:Boolean = false;
			var l:int = viewChildrenNum;
			if (l > 0) {
				for (var i:int = 0; i < l; i++) {
					r = _views[i] == view;
					if (r) break;
				}
			}
			
			return r;
		}
		
		public function get viewChildren():Array { return _views; }
		
		public function get viewChildrenNum():int { return _views.length; }
		
		/////////////////////////////////////////
		// Protected Methods ////////////////////
		/////////////////////////////////////////
		
		protected function invalidateAll():void {
			if(_views.length > 0) {
				var l:int = _views.length;
				var v:View;
				for (var i:int = 0; i < l; i++) {
					v = _views[i];
					v.invalidate();
				}
			}
		}
		
		/////////////////////////////////////////
		// Properties ///////////////////////////
		/////////////////////////////////////////
		
		/////////////////////////////////////////
		// Private Methods //////////////////////
		/////////////////////////////////////////
		
		
	}

}